{extend name="admin@public/dialog" /}
{block name="styles"}
<link rel="stylesheet" type="text/css" href="__PUBLIC__/webupload/webuploader.css">
<link rel="stylesheet" type="text/css" href="__PUBLIC__/webupload/demo/file_style.css">
<style>
   .webuploader-container {
	position: relative;
}
.webuploader-element-invisible {
	position: absolute !important;
	clip: rect(1px 1px 1px 1px); /* IE6, IE7 */
    clip: rect(1px,1px,1px,1px);
}
.webuploader-pick {
	position: relative;
	display: inline-block;
	cursor: pointer;
	background: #00b7ee;
	padding: 10px 15px;
	color: #fff;
	text-align: center;
	border-radius: 3px;
	overflow: hidden;
}
.webuploader-pick-hover {
	background: #00a2d4;
}

.webuploader-pick-disable {
	opacity: 0.6;
	pointer-events:none;
} 

#ctlBtn {
    border:none;
    font-size: 16px;
    border-radius: 3px;
}
.btns {
    padding-left: 35%;
}
</style>
{/block}
{block name="main-content"}
<div id="uploader" class="wu-example">
    <!--用来存放文件信息-->
    <div id="thelist" class="uploader-list"></div>
    <div class="btns">
        <div id="picker">选择文件</div>
        <button id="ctlBtn" class="btn btn-default" >开始上传</button>
    </div>
</div>
{/block}
{block name="scripts"}
<script src="__PUBLIC__/webupload/webuploader.js"></script>
<script src="__PUBLIC__/layer/layer_zh-cn.js"></script>
<script>
 jQuery(function() {
    var $ = jQuery,
        $list = $('#thelist'),
        $btn = $('#ctlBtn'),
        state = 'pending',
        uploader;

    uploader = WebUploader.create({

        // 不压缩image
        resize: false,
        // swf文件路径
        swf: "__PUBLIC__/webupload/Uploader.swf",

        // 文件接收服务端。
        server: "{:url('admin/Upload/upload')}",
        formData: {
            formInfo:'{$formInfo?$formInfo:""}'
        },
        accept: {
            title: 'Files',
            extensions: 'doc,docx,xls,xlsx,pdf,rar,zip,jpg,png',
            mimeTypes: 'application/msword,application/vnd.ms-excel,application/pdf,application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.openxmlformats-officedocument.wordprocessingml.document'
        },

        // 选择文件的按钮。可选。
        pick: '#picker',
        fileNumLimit: 300,
        fileSizeLimit: 200 * 1024 * 1024,    // 200 M
        fileSingleSizeLimit: 50 * 1024 * 1024    // 50 M
    });

    // 当有文件添加进来的时候
    uploader.on( 'fileQueued', function( file ) {
        $list.append( '<div id="' + file.id + '" class="item">' +
            '<h4 class="info">' + file.name + '</h4>' +
            '<p class="state">等待上传...</p>' +
        '</div>' );
    });

    // 文件上传过程中创建进度条实时显示。
    uploader.on( 'uploadProgress', function( file, percentage ) {
        var $li = $( '#'+file.id ),
            $percent = $li.find('.progress .progress-bar');

        // 避免重复创建
        if ( !$percent.length ) {
            $percent = $('<div class="progress progress-striped active">' +
              '<div class="progress-bar" role="progressbar" style="width: 0%">' +
              '</div>' +
            '</div>').appendTo( $li ).find('.progress-bar');
        }

        $li.find('p.state').text('上传中');

        $percent.css( 'width', percentage * 100 + '%' );
    });

    uploader.on( 'uploadSuccess', function( file,response) {
        if (response.code == 1){
           $( '#'+file.id ).find('p.state').text(response.msg); 
        } else {
           $( '#'+file.id ).find('p.state').text('已上传');  
        }
        
    });

    uploader.on( 'uploadError', function( file ) {
        $( '#'+file.id ).find('p.state').text('上传出错');
    });

    uploader.on( 'uploadComplete', function( file ) {
        $( '#'+file.id ).find('.progress').fadeOut();
    });

    uploader.on( 'all', function( type ) {
        if ( type === 'startUpload' ) {
            state = 'uploading';
        } else if ( type === 'stopUpload' ) {
            state = 'paused';
        } else if ( type === 'uploadFinished' ) {
            state = 'done';
        }

        if ( state === 'uploading' ) {
            $btn.text('暂停上传');
        } else {
            $btn.text('开始上传');
        }
    });

    $btn.on( 'click', function() {
        if ( state === 'uploading' ) {
            uploader.stop();
        } else {
            uploader.upload();
        }
    });
});
</script>
{/block}